<?php
/**
 * 登录公共类.
 * User: ws
 * Date: 2018/7/20
 * Time: 14:47
 */

namespace app\admin\controller;

use think\Db;
class Login extends Init
{
    function _initialize()
    {
        parent::_initialize();
    }

    public function login()
    {
        if(request()->isPost()){
            $params = input('post.');
            if(!captcha_check($params['captcha'])){
                return json(array('code'=>0,'msg'=>'验证码不正确'));
            };
            $result = model('admin_user')->do_login($params);
            if($result){
                $where['name'] = $params['username'];
                $where['state'] = 1;
                $admin_state = Db::name('admin_user')->where($where)->find();
                if(!$admin_state) {
                    return json(array('code'=>0,'msg'=>'账号已被禁用'));
                }
                return json(array('code'=>200,'msg'=>'登陆成功'));
            }else{
                return json(array('code'=>0,'msg'=>'用户名或者密码不正确'));
            }
        }
        if(session('?admin_user')){
            $this->redirect('index/index');
        }
        return view('public/login');
    }

    public function logout()
    {
        session(null);
        if(!session('?admin_user')){
            return json(array('code'=>200,'msg'=>'退出成功'));
        }else{
            return json(array('code'=>0,'msg'=>'退出失败'));
        }
    }



}